package com.cloud.tsp.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.annotation.TransUser;
import com.cloud.api.domain.screen.MapData;
import com.cloud.api.domain.shop.SpuMerchant;
import com.cloud.api.domain.sys.DeptExtend;
import com.cloud.api.domain.sys.DeptExtendQuery;
import com.cloud.api.domain.sys.ReturnAddrRequest;
import com.cloud.api.domain.sys.SysDept;
import com.cloud.api.constants.StrVo;
import com.cloud.mybatis.domain.TableDataInfo;
import com.cloud.tsp.mapper.DeptExtendMapper;
import com.cloud.tsp.service.IDeptExtendService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-09-13
 */
@Service("deptExtendService")
public class DeptExtendServiceImpl extends ServiceImpl<DeptExtendMapper, DeptExtend> implements IDeptExtendService {

    /*@Resource
    private IDeptOpenFeignService deptOpenFeignService;*/

    /*@Override
    public void updateDep(DeptExtend para) {
        para.setParentId(null);
        SysDept sysDept = new SysDept();
        sysDept.setDeptId(para.getDeptId());
        sysDept.setDeptName(para.getDeptName());
        sysDept.setLeader(para.getContactMan());
        sysDept.setPhone(para.getContact());
        sysDept.setDescription(para.getDescription());
        this.updateById(para);
        deptOpenFeignService.insertOrUpdate(sysDept);
    }*/

    @Override
    public void updateReturnAddr(ReturnAddrRequest para) {
        DeptExtend deptExtend = new DeptExtend();
        BeanUtils.copyProperties(para,deptExtend);
        baseMapper.updateById(deptExtend);
    }

    /*@Override
    public void deleteById(StrVo para) {
     deptOpenFeignService.deleteByIdForExtend(para);
     baseMapper.deleteById(para.getId());
    }*/

    @Override
    @TransUser
    public TableDataInfo<DeptExtend> selectDeptExtendList(DeptExtendQuery para) {
        String[] deptType = para.getDeptType().split(",");
        para.setDeptTypes(deptType);
        Page<DeptExtend> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<DeptExtend> data = baseMapper.selectDeptExtendList(page, para);
        return TableDataInfo.build(data);
    }

    @Override
    public List<SpuMerchant> selectMerchantsForSpu() {
        List<SpuMerchant> spuMerchants = new ArrayList<>();
        List<DeptExtend> straightSale = baseMapper.selectMerchantsForSpu("sale", "1");
        SpuMerchant straightSaleMerchant = new SpuMerchant();
        straightSaleMerchant.setDeptId("1");
        straightSaleMerchant.setDeptName("直营店");
        straightSaleMerchant.setChildren(straightSale);
        spuMerchants.add(straightSaleMerchant);

        List<DeptExtend> sale = baseMapper.selectMerchantsForSpu("sale", "0");
        SpuMerchant saleMerchant = new SpuMerchant();
        straightSaleMerchant.setDeptId("2");
        saleMerchant.setDeptName("经销店");
        saleMerchant.setChildren(sale);
        spuMerchants.add(saleMerchant);

        List<DeptExtend> service = baseMapper.selectMerchantsForSpu("service",null);
        SpuMerchant serviceMerchant = new SpuMerchant();
        straightSaleMerchant.setDeptId("3");
        serviceMerchant.setDeptName("服务站");
        serviceMerchant.setChildren(service);
        spuMerchants.add(serviceMerchant);

        return spuMerchants;
    }

    @Override
    public TableDataInfo<DeptExtend> selectStationList(DeptExtendQuery para) {
        para.setDeptType("service");
        Page<DeptExtend> page = new Page<>();
        page.setCurrent(para.getPage());
        page.setSize(para.getPageSize());
        IPage<DeptExtend> data = baseMapper.selectStationList(page, para);
        return TableDataInfo.build(data);
    }

    @Override
    public List<MapData> getDeptCountMap() {
        return baseMapper.getDeptCountMap();
    }
}